import { getToken } from '@/utils/auth'
import { getInfo } from '@/api/login'
import { guestLayout, userLayout } from '@/api/portal/user/layout'
import { requestGet } from '@/api/system/common'

export const query = {
  data() {
    return {
      dataList: [],
      baseURL: process.env.VUE_APP_BASE_API.substring(
        0,
        process.env.VUE_APP_BASE_API.length - 1
      ),
      form: {
        blurry: ''
      }
    }
  },
  computed: {
    ContentBack() {
      return { background: 'url(' + this.layoutTheme.image + ') repeat', backgroundSize: '100%' }
    },
    headerBorder() {
      return {
        '--border-color': this.layoutTheme.menuBorderColor,
        '--active-color': this.layoutTheme.menuActiveColor,
        '--menu-bak-color': this.layoutTheme.menuBackground
      }
    },
    contentStyle() {
      return { '--background-color': this.layoutTheme.backgroundColor }
    }
  },
  mounted() {
    // 查询布局
    this.initLayout()
    // 设置用户基本信息
    this.initUser()
  },
  created() {
    const title = this.$route.query.title
    if (undefined !== title && title !== null && title !== '') {
      this.form.blurry = title
    }
    this.initService()
  },
  methods: {
    initUser() {
      if (getToken()) {
        getInfo().then(res => {
          this.$store.commit('SET_USER', res.user)
        }).catch(() => {

        })
      }
    },
    // 查询布局信息
    initLayout() {
      if (getToken()) {
        userLayout().then(res => {
          const { data } = res
          this.$store.commit('SET_LAYOUT', data)
        })
      } else {
        guestLayout().then(res => {
          const { data } = res
          this.$store.commit('SET_LAYOUT', data)
        })
      }
    },
    // 查询服务
    initService() {
      if (getToken()) {
        requestGet('rest/api/service/v1/all/analysis/default/auth/all', this.form)
          .then(res => {
            const { data } = res
            this.dataList = data.data
          }).catch(() => {
            this.dataList = []
          })
      }
    },
    queryServer(data) {
      this.form.blurry = data
      this.initService()
    },
    openEntity(data) {
      window.open(data.openUrl, '_blank')
    }
  }
}
